﻿using System.Collections;
using System;


namespace Chess_Model
{

    public class Knight : Piece
    {
        //constructor to set the position of the piece and the color of the player
        public Knight(Position position, int play)
        {
            player = play;
            pos = position;
        }
        // Generates a list of new legal positions from the current position
        // regardless of the other pieces' positions
        public override ArrayList getNextLegalMoves()
        {
            //use to hold x and y coordinate of the current piece
            int xcoord, ycoord;
            //use to hold the current position of this knight
            Position newpos;
            //getting the position of night then assigning it to x and y coordinate variables
            newpos = this.getCurrentPosition();
            xcoord = newpos.x;
            ycoord = newpos.y;

            

            //creating an array of Position class to hold the position
            Position[] ListofMoves = new Position[8];
           
            //setting the x coordinate
            for (int i = 0; i < 8; i++)
            {
                if (i < 4)//while the index is 0-3 you increment x by either 1 or -1
                {
                    if (i % 2 == 0)//use to alternate the signs
                    {
                        ListofMoves[i] = new Position(xcoord + 1, 0);
                    }
                    else
                    {
                        
                        ListofMoves[i] = new Position(xcoord - 1 , 0);
                    }
                }
                else//if the index is 4-7 then increment x by 2 or -2
                {
                    if (i % 2 == 0)
                    {
                        ListofMoves[i] = new Position(xcoord + 2, 0);
                    }
                    else
                    {
                        ListofMoves[i] = new Position(xcoord - 2, 0);
                    }
                }
                
            }


            //use to set the y coordinate of the list
            for (int i = 0; i < 8; i++)
            {
                if (i < 4)//while the index is less than 4 increment by 2 or -2
                {
                    if (i < 2)//use to add the first two than subtract the second two
                    {
                        ListofMoves[i] = new Position(ListofMoves[i].x,ycoord+2);
                    }
                    else
                    {
                        
                        ListofMoves[i] = new Position(ListofMoves[i].x, ycoord - 2);
                    }
                }
                else
                {
                    if (i < 6)
                    {
                        
                        ListofMoves[i] = new Position(ListofMoves[i].x,ycoord +1);
                    }
                    else
                    {
                        ListofMoves[i] = new Position(ListofMoves[i].x,ycoord -1 );
                    }
                }
                
            }

            //checking for boundaries and adding only the one in bounds
            ArrayList mylist = new ArrayList();
            for (int i = 0; i < 8; i++)
            {
                //if it is not out of bounds then add
                if (!((ListofMoves[i].x < 0) || (ListofMoves[i].x > 7) || (ListofMoves[i].y < 0) || (ListofMoves[i].y > 7)))
                    mylist.Add(ListofMoves[i]);
            }

          
            return mylist;
               
        }

        public override String toString()
        {
            return String.Format("{0}{1}", "K", player);
        }
       

    }

}
